import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 涛
 * Date: 2023-10-01
 * Time: 15:01
 */
class ListNode {
    int val;
    ListNode head;
    ListNode last;
    ListNode prev;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
class question1 {
    public boolean chkPalindrome(ListNode head) {
        if(head == null || head.next == null) {
            return true;
        }
        // write code here
        ListNode fast = head;
        ListNode slow = head;
        //找到中间位置
        while(fast != null && fast.next != null ) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //中间位置
        ListNode cur = slow.next;
        //翻转
        while(cur != null) {
            ListNode curNext =cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //从前往后，从后往前
        while(head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }
}
//双向链表尾插
public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            last = node;
        } else {

            last.next = node;
            node.prev = last;
            last = last.next;
        }
}
public class Test1 {
    public static void main(String[] args) {

    }
}
